package cn.com.generaldata.os.service.indicator_evaluation.coefficient_of_variation;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 产业结构变动度变异系数评估指标计算类.
 */
public class IndustryStructureChangeVariatCalculateService extends IndicatorEvaluationCalculateService {

	private String[] cityAreas = ProvinceIndicatorConstant.CITYS;
	private String industryChange=ProvinceIndicatorConstant.INDUSTRY_STRUCT_CHANGE_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		IndicatorAccountVO iav = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		Double ySum = 0d;
		Map cityLEMap = new HashMap();
		for (String city : cityAreas) {
			iav.setAreaId(city);
			//城市产业结构变动度
			Double industryStructureCity = getOceanRelatedEmployedContribution(iav,industryChange);
			cityLEMap.put(city, industryStructureCity);
			ySum += industryStructureCity;
		}
		Double yAverage = ySum / 3;
		Double sum = 0d;
		for (String city : cityAreas) {
			Double cityLocationEntropy = (Double) cityLEMap.get(city);
			sum += Math.pow((cityLocationEntropy - yAverage), 2);
		}
		return Math.sqrt(sum / 3) / yAverage;
	}
	private Double getOceanRelatedEmployedContribution(IndicatorAccountVO indicatorAccountVO, String name) {
		Integer indicatorInputId = getIndicatorEvaluationId(name, indicatorAccountVO.getAreaId());

		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorInputId);
		Double evaluationIndicatorValue = getEvaluationIndicatorValue(cri);
		Validate.isTrue(evaluationIndicatorValue != null);
		return evaluationIndicatorValue;
		
	}
}
